﻿using UnityEngine;
using System.Collections;
using COC;
using COC.CEvent;
using System;
using COC.Window;
using COC.Character;
public class SkeletonKnight_Backup_v1 : BaseBehaviourSupport
{
		private Animator anim;
		private int MOUSE_LEFT = 0;
		private UILabel info;
		private UILabel sourceGo;
		public Transform skeletonKnight;
		CEventDriven cdriver;
		bool enableEvent = false;
		int i = 0;
		bool showText;

		public override void OnStart ()
		{
				this.gameObject.AddComponent<CServiceActivator> ();
				enableEvent = true;
		}

		public override void OnAwake ()
		{
				//Debug.Log(new TestService().ToString());
				//Debug.Log ("" + (((0x0001 | 8) >> 32) << 32));
				//Debug.Log(""+Convert.ToString(10,2));
				//Debug.Log(""+((CEvent.ACTION_AUDIO_PLAY>>32)<<32));
				Debug.Log (Application.platform.ToString ());

				anim = GetComponent<Animator> ();
				if (anim.layerCount > 1) {
						anim.SetLayerWeight (1, 1);
				}
				info = GameObject.FindGameObjectWithTag ("information_label").GetComponent<UILabel> ();
		        
				sourceGo = GameObject.FindGameObjectWithTag ("information_label2").GetComponent<UILabel> ();
				showText = true;
				//	StartCoroutine ("CloneSK");
		}
		// Use this for initialization
		//void Update()
		//{
		//	}

		void LateUpdate ()
		{
				//CloneSK ();
				//  InstantiateWithScriptByMouseOrTouch(skeletonKnight,Input.mousePosition,"MainCamera",0,"Terrain",false);
				if (!enableEvent) {
						StartCoroutine ("CTestEventHandler");
				}    
		}

		void OnDestroy ()
		{
				StopCoroutine ("CloneSK");

		}
		
		int touched = 0;
		int id = 0;
		// Update is called once per frame
		void CloneSK ()
		{
				#if UNITY_ANDROID_1
		//info.text=Input.touches.Length+"";
		if(Input.touchCount==1){
			if(Input.GetTouch(i).phase==TouchPhase.Began){
				info.text="touched:"+Input.touches.Length;
				GameObject go=Instantiate(this.gameObject,transform.position,transform.rotation) as GameObject;
				go.transform.Translate(new Vector3(this.gameObject.transform.position.x+UnityEngine.Random.value*10,
				                                   this.gameObject.transform.position.y,
				                                   this.gameObject.transform.position.z));
				//go.transform.position.x=(this.gameObject.transform.position.x+UnityEngine.Random.value*10);
				Debug.Log("Have Touched "+touched+"Times");
			}
		}
				#elif UNITY_IPHONE
		
				#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
		
				#else
		
				#endif
				sourceGo.text = "Current Position:" + "x = " + transform.position.x + ";" +
						"y = " + transform.position.y + "" +
						";z = " + transform.position.z;

				//Debug.Log ("" + Input.GetMouseButton (0) + ";" + Input.GetMouseButton (1));
				if (Input.GetMouseButtonUp (MOUSE_LEFT) && enableAllScripts && GUIUtility.hotControl == 0) {
						Vector3 mousePos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
						Vector3 wordPos;
						Camera currCamera = GameObject.FindWithTag ("MainCamera").camera;
						Ray ray = currCamera.ScreenPointToRay (mousePos);
						RaycastHit hit; 
						if (Physics.Raycast (ray, out hit,
			                     Mathf.Infinity)) {
								wordPos = hit.point;               
								if (hit.collider.tag == "Terrain") {
										Debug.DrawLine (hit.point, Input.mousePosition, Color.red);
										//wordPos.y += 0.5f;
										wordPos.x = Mathf.Round (wordPos.x);
										wordPos.y = Mathf.Round (wordPos.y);
										wordPos.z = Mathf.Round (wordPos.z);
										
										info.text = "x = " + wordPos.x + ";y = " + wordPos.y + " ;z = " + wordPos.z;

								}

								Transform t = (Transform)Instantiate (skeletonKnight, wordPos, Quaternion.identity);
				
								t.name = transform.name + "_" + (++id);
								t.GetComponent<SkeletonKnight_Backup_v1> ().enabled = false;
								t.GetComponent<SkeletonKnight_Backup_v1> ().enableAllScripts = false;
								t.GetComponent<SkeletonKnight_Backup_v1> ().enabled = true;
								t.localScale = transform.localScale;
						} else {
								wordPos = currCamera.ScreenToWorldPoint (mousePos);
				
						}

				}
		}

		IEnumerator CTestEventHandler ()
		{
				if (enableAllScripts) {
						CEventHandler handler = new CEventHandler (CEventType.UI_EVENT, this, CEvent.ACTION_UI_DEFAULT);
						GameObject go = GameObject.FindGameObjectWithTag (CEventDriven.ALIAS_NAME);
						if (go != null) {
				
						} else {
								go = GameObject.Find (CEventDriven.ALIAS_NAME);
				
						}
						cdriver = go.GetComponent<CEventDriven> ();
						cdriver.HandleMessage (handler);
						CEventHandler handler1 = new CEventHandler (CEventType.UI_EVENT, this, CEvent.ACTION_UI_DEFAULT);
						handler1.action = CEvent.ACTION_AUDIO_PAUSE;
						cdriver.HandleMessage (handler1);
						CEventHandler handler2 = new CEventHandler (CEventType.UI_EVENT, this, CEvent.ACTION_UI_DEFAULT);
						handler2.action = CEvent.ACTION_UI_DISABLE;
						cdriver.HandleMessage (handler2);
						CEventHandler handler3 = new CEventHandler (CEventType.UI_EVENT, this, CEvent.ACTION_UI_DEFAULT);
						handler3.action = CEvent.ACTION_UI_UPDATE_DATA;
						cdriver.HandleMessage (handler3);
						cdriver.getAvailableServicesCount ();
						//cdriver.RemoveService (typeof(TestService));
						cdriver.getAvailableServicesCount ();
						//reset to avoid to duplicate repeatedly
						enableEvent = false;
						yield return cdriver;
				}
				yield return null;
		}


}
